﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Color = System.Windows.Media.Color;

/////////////////////////////////////////////////////////////////////////////
namespace ThinControlLibrary
{
    /////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Interaction logic for SongTitleControl.xaml
    /// </summary>
    public partial class SongTitleControl : UserControl
    {
        private const double AnimationTime = 6; //in seconds

        private Label _label;
        private Canvas _container;
        private string _text;

        /////////////////////////////////////////////////////////////////////
        public SongTitleControl()
        {
            InitializeComponent();
            this.Loaded += (sender, args) => _init();
        }

        /////////////////////////////////////////////////////////////////////
        public string Text
        {
            get { return _text; }
            set
            {
                if (_label != null)
                {
                    _label.Content = _text;
                }
                _text = value;
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _init()
        {
            _label = CaptionContentControl.Template.FindName("songLabel", CaptionContentControl) as Label;
            _container = CaptionContentControl.Template.FindName("DigitalCanvas", CaptionContentControl) as Canvas;
            
            if(_label != null && _container != null)
            {
                _label.Content = _text;

                _initAnimatin();
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _initAnimatin()
        {
            NameScope.SetNameScope(this, new NameScope());
            Color textColor = Color.FromArgb(57, 24, 186, 239);
            Color blinkColor = Color.FromArgb(255, 24, 186, 239);

            LinearGradientBrush gradientBrush = new LinearGradientBrush();
            GradientStop point0 = new GradientStop(textColor, 0.0);
            GradientStop pointA = new GradientStop(textColor, -0.2);
            GradientStop pointB = new GradientStop(blinkColor, -0.1);
            GradientStop pointC = new GradientStop(textColor, 0.0);
            GradientStop pointD = new GradientStop(textColor, 1.0);

            this.RegisterName("point0", point0);
            this.RegisterName("pointA", pointA);
            this.RegisterName("pointB", pointB);
            this.RegisterName("pointC", pointC);
            this.RegisterName("pointD", pointD);

            gradientBrush.GradientStops.Add(point0);
            gradientBrush.GradientStops.Add(pointA);
            gradientBrush.GradientStops.Add(pointB);
            gradientBrush.GradientStops.Add(pointC);
            gradientBrush.GradientStops.Add(pointD);

            _label.Foreground = gradientBrush;

            DoubleAnimation gradientPointAAnimation = new DoubleAnimation
            {
                From = -0.4,
                To = 1.2,
                Duration = TimeSpan.FromSeconds(AnimationTime),
                AutoReverse = false,
                BeginTime = TimeSpan.FromSeconds(0),
                RepeatBehavior = RepeatBehavior.Forever
            };
            Storyboard.SetTargetName(gradientPointAAnimation, "pointA");
            Storyboard.SetTargetProperty(gradientPointAAnimation, new PropertyPath(GradientStop.OffsetProperty));

            DoubleAnimation gradientPointBAnimation = new DoubleAnimation
            {
                From = -0.3,
                To = 1.3,
                Duration = TimeSpan.FromSeconds(AnimationTime),
                AutoReverse = false,
                BeginTime = TimeSpan.FromSeconds(0),
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTargetName(gradientPointBAnimation, "pointB");
            Storyboard.SetTargetProperty(gradientPointBAnimation, new PropertyPath(GradientStop.OffsetProperty));

            DoubleAnimation gradientPointCAnimation = new DoubleAnimation
            {
                From = -0.2,
                To = 1.4,
                Duration = TimeSpan.FromSeconds(AnimationTime),
                AutoReverse = false,
                BeginTime = TimeSpan.FromSeconds(0),
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTargetName(gradientPointCAnimation, "pointC");
            Storyboard.SetTargetProperty(gradientPointCAnimation, new PropertyPath(GradientStop.OffsetProperty));

            Storyboard gradientAnimation = new Storyboard();
            gradientAnimation.Children.Add(gradientPointAAnimation);
            gradientAnimation.Children.Add(gradientPointBAnimation);
            gradientAnimation.Children.Add(gradientPointCAnimation);
            gradientAnimation.Begin(this);
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//ThinControlLibrary
